<?php

namespace App\Repository;

use App\Consts\GlobalConst;
use App\Consts\GlobalSalonConst;
use App\Models\IndexSalon;
use App\Models\Select\SalonSelect;
use App\Models\SysSalon;
use App\Models\SysSalonContent;

/**
 * Class SalonRepository
 * @package App\Repository
 */
class SalonRepository extends BaseRepository
{
    /**
     * 获取沙龙列表
     * @param $searchParams
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getSalonList($searchParams)
    {
        $provider = IndexSalon::getInstance()
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
            ->where('is_enable', GlobalSalonConst::IS_ENABLED);

        if ($searchParams['is_review'] == GlobalSalonConst::IS_NOT_REVIEW) {
            //列表顶部数据【除往期回顾】
            $provider->where('is_review', GlobalSalonConst::IS_NOT_REVIEW);
            $provider->orWhere([
                ['is_deleted', GlobalConst::IS_NOT_DELETE],
                ['is_enable', GlobalSalonConst::IS_ENABLED],
                ['is_review', GlobalSalonConst::IS_REVIEW],
                ['end_at', '>', time()],
            ]);
        } elseif ($searchParams['is_review'] == GlobalSalonConst::IS_REVIEW) {
            //往期回顾
            $provider->where('is_review', GlobalSalonConst::IS_REVIEW);
            $provider->where('end_at', '<', time());
            $provider->orderByDesc('start_at');
        }

        return $this->setPagination($provider);
    }

    /**
     * 通过分表数，获取沙龙基础信息
     * @param $tableIndex
     * @param $salonId
     * @param string $selectScene
     * @return SysSalon[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model[]
     */
    public function getSalonByTableCount($tableIndex, $salonId, $selectScene = SalonSelect::BASE_INFO)
    {
        $tableName = SysSalon::getTableNameByTableIndex($tableIndex);
        $noteId = (array)$salonId;

        return SysSalon::setTableName($tableName)
            ->select(SalonSelect::getSelects($selectScene))
            ->whereIn('salon_id', $noteId)
            ->get();
    }

    /**
     * 根据沙龙ID获取沙龙记录
     * @param $salonId
     * @param string $selectScene
     * @return SysSalon|\Illuminate\Database\Eloquent\Builder|object|null
     */
    public function getSalonBySalonId($salonId, $selectScene = SalonSelect::SELF_DETAIL)
    {
        return SysSalon::getInstance($salonId)
            ->select(SalonSelect::getSelects($selectScene))
            ->where('salon_id', $salonId)
            ->first();
    }

    /**
     * 根据沙龙ID获取沙龙文本记录
     * @param $salonId
     * @param array|string $selects
     * @return SysSalonContent|\Illuminate\Database\Eloquent\Builder|object|null
     */
    public function getSalonContentBySalonId($salonId, $selects = ['*'])
    {
        return SysSalonContent::getInstance($salonId)
            ->select($selects)
            ->where('salon_id', $salonId)
            ->first();
    }
}
